package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgCustomerCompany;
import com.myteam.fugaocrm.service.CustomerCompanyServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerCompanyController {
	@Resource protected CustomerCompanyServiceImpl customerCompanyServiceImpl;
	
	@RequestMapping("customer/company/all")
	@ResponseBody
	public ModelAndView all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<FgCustomerCompany> fgCustomerCompanyList = customerCompanyServiceImpl.all();
		
		ModelAndView view = new ModelAndView();
		view.addObject("fgCustomerCompanyList", fgCustomerCompanyList);
		view.setViewName("arch_customer_company.jsp");
		return view;
	}
	
	@RequestMapping("customer/company/delete")
	@ResponseBody
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer customerCompanyId = Integer.parseInt(request.getParameter("companyId").toString());
		
		customerCompanyServiceImpl.deleteOne(customerCompanyId);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/customer/company/all");
		return view;
	}
	
	@RequestMapping("customer/company/insertorupdate")
	@ResponseBody
	public ModelAndView insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String customerCompanyIdStr = request.getParameter("companyId")+"";
		String customerCompanyName = request.getParameter("companyName")+"";
		String customerCompanyAdress = request.getParameter("companyAdress")+"";
		String industryName = request.getParameter("companyIndustry")+"";
		
		//校验
		
		
		//插入或更新
		Date date = new Date();
		String staffName = request.getSession().getAttribute("staffName")+"";
		Map<String, Object> params = new HashMap<String, Object>();
		
		if("".equals(customerCompanyIdStr)) {
			//insert
			params.put("customerCompanyName", customerCompanyName);
			params.put("customerCompanyAdress", customerCompanyAdress);
			params.put("industryName", industryName);
			params.put("createdDate", date);
			params.put("createdUser", staffName);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			customerCompanyServiceImpl.insert(params);
		}else {
			//update
			Integer customerCompanyId = Integer.parseInt(customerCompanyIdStr);
			params.put("customerCompanyId", customerCompanyId);
			params.put("customerCompanyName", customerCompanyName);
			params.put("customerCompanyAdress", customerCompanyAdress);
			params.put("industryName", industryName);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			customerCompanyServiceImpl.update(params);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/customer/company/all");
		return view;
	}
}
